<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcom' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCateUser">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格数据 -->
      <tree-table
        class="treeTable"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        border
      >
        <!-- 是否有效 -->
        <template slot="isMoban" scope="scope">
          <i
            class="el-icon-circle-check"
            v-if="scope.row.cat_deleted == false"
            style="color: green"
          ></i>
          <i
            class="el-icon-circle-close"
            v-if="scope.row.cat_deleted == true"
            style="color: red"
          ></i>
        </template>
        <!-- 分级 -->
        <template slot="order" scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag
            size="mini"
            v-else-if="scope.row.cat_level == 1"
            type="success"
            >二级</el-tag
          >
          <el-tag size="mini" v-else type="danger">三级</el-tag>
        </template>
        <!-- 操作-->
        <template slot="power" scope="scope">
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="setCaet(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="deleteCate(scope.row)"
            >删除</el-button
          >
        </template>
      </tree-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[5, 1, 2, 3, 4]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类 -->

    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialog"
      width="40%"
      @close="clear"
    >
      <el-form
        :model="ruleFormCate"
        :rules="ruleFormCateRules"
        ref="ruleCateForm"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleFormCate.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            clearable
            :props="cateCascader"
            v-model="parentCate"
            :options="secondList"
            @change="handleChange"
            emitPath='false'
           change-on-select="true"
          >
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialog = false">取 消</el-button>
        <el-button type="primary" @click="addCateUserPost">确 定</el-button>
      </span>
    </el-dialog>

  

    <!-- 编辑对话框 -->
    <el-dialog title="编辑信息" :visible.sync="setCaetShow" width="40%">
      <!-- 编辑对话框 -->

      <el-form
        :model="setCateForm"
        :rules="ruleFormCateRules"
        ref="RulesFormRef"
        label-width="80px"
      >
        <el-form-item
          label="编辑分类名称"
        
          label-width="100px"
        >
          <el-input v-model="setCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="setCaetShow = false">取 消</el-button>
        <el-button type="primary" @click="setCateUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询条件
      queryinfo: {
        type: 3, // 列表值
        pagenum: 1, //当前页码值
        pagesize: 5, //每页显示数据条数
      },
      //商品分类列表,默认为空
      cateList: [],
      //数据总条数
      total: 0,
      // 为table指定列的定义
      columns: [
        {
          label: '分类名称', //列名称
          prop: 'cat_name', //商品分类名称
        },
        {
          label: '是否有效',
          //表示当前列定义为模板列
          type: 'template',
          //表示当前这一列使用的模板名称
          template: 'isMoban',
        },
        {
          label: '等级',
          //表示当前列定义为模板列
          type: 'template',
          //表示当前这一列使用的模板名称
          template: 'order',
        },
        {
          label: '操作',
          //表示当前列定义为模板列
          type: 'template',
          //表示当前这一列使用的模板名称
          template: 'power',
        },
      ],
      //添加分类是否显示
      addCateDialog: false,
      //默认数据
      ruleFormCate: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0,
      },
      //验证规则
      ruleFormCateRules: {
        cat_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur',
          },
        ],
      },
      //两级菜单
      secondList: [],
      //v-model 绑定的 选中的父级的Id数组
      parentCate: [],
      //props 指定值
      cateCascader: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        // checkStrictly:'true'
      },
      //显示编辑对话框
      setCaetShow: false,
      //编辑对话框的内容
      setCateForm: {},
      setId: '',
    }
  },

  created() {
    this.getCateList()
  },
  methods: {
    //获取商品分类数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        //使用params指定查询参数
        params: this.queryinfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('商品获取失败')
      }
      // 获取商品成功
      this.cateList = res.data.result
      // console.log( this.cateList);
      // 为总数据条数赋值
      this.total = res.data.total
    },
    //每页多少个
    handleSizeChange(newsize) {
      this.queryinfo.pagesize = newsize
      this.getCateList()
    },
    //页码
    handleCurrentChange(newpage) {
      this.queryinfo.pagenum = newpage
      this.getCateList()
    },
    // 添加分类显示隐藏
    async addCateUser() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 },
      })
      this.secondList = res.data
      this.addCateDialog = true
    },
    //级联选择器发生变化
    handleChange() {
      console.log(this.parentCate)
      // 如果有长度说名有父级
      if (this.parentCate.length > 0) {
        this.ruleFormCate.cat_pid = this.parentCate[this.parentCate.length - 1]
        //  为当前分类等级赋值
        this.ruleFormCate.cat_level = this.parentCate.length
        return
      } else {
        this.ruleFormCate.cat_pid = this.ruleFormCate.cat_level = 0
      }
    },
    //添加分类
    async addCateUserPost() {
      // this.$refs.ruleCateForm.validate(async (val) => {
      //   if (!val) return

      const { data: res } = await this.$http.post(
        'categories',
        this.ruleFormCate
      )
      if (res.meta.status !== 201) {
        return this.$message.error('添加分类失败')
      }
      this.$message.success('添加分类成功')
      this.getCateList()
      this.addCateDialog = false
      // })
    },
    //退出清空
    clear() {
      this.$refs.ruleCateForm.resetFields()
      this.ruleFormCate.cat_pid = ''
      this.ruleFormCate.cat_level = ''
      this.parentCate = []
    },
    //删除分类
    async deleteCate(id) {
      this.setId = id.cat_id
      const confirmrsult = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      //如果确认删除 confirm
      //取消 cancel
      console.log(confirmrsult)
      if (confirmrsult != 'confirm') return this.$message.info('已经取消删除')

      const { data: res } = await this.$http.delete('categories/' + id.cat_id)

      if (res.meta.status != 200) return this.$message.error('删除失败')

      this.$message.success('删除成功！')

      this.getCateList()
    },
    //编辑对话框
    async setCaet(id) {
      //cat_id
      const { data: res } = await this.$http.get('categories/' + id.cat_id)
      this.setCateForm = res.data
      this.setCaetShow = true
    },
    //点击确定编辑更改数据
    setCateUser() {
      this.$refs.RulesFormRef.validate(async (valid) => {
        console.log(valid)
        if (!valid) return

        const { data: res } = await this.$http.put('categories/' + this.setCateForm.cat_id, {
           cat_name: this.setCateForm.cat_name

        })
  
        if (res.meta.status != 200) return this.$message.error('更新编辑失败！')

         this.getCateList()
          this.setCaetShow = false

        this.$message.success('更新编辑成功')
      })
    },
  },
}
</script>

<style>
.treeTable {
  margin-top: 40px;
}
.el-cascader-menu {
  width: 100%;
  height: 500px;
}
</style>